import numpy as np
import pandas as pd

def create_linear_noise(normal_data):
	'''
	add linear anomaly artificially and marks
	:return: np array with shape [None, 16], and add the last column 'Class' which denotes
	weather the current row is outlier

	添加四个维度的异常信息：
	维度：0-3
	位置：1200-1290
	添加标签class：0 -> 正常
			      1 -> 直线型异常
	'''
	maxs = np.max(normal_data, axis=0)
	mins = np.min(normal_data, axis=0)

	for i in range(4):
		max_i = maxs[i]
		for j in range(90):
			normal_data[1200 + j, i] = max_i

	for i in range(4):
		min_i = mins[i]
		for j in range(200):
			normal_data[14000 + j, i] = min_i

	# 添加Class标签
	tags = np.zeros(normal_data.shape[0])
	for i in range(90):
		tags[1200 + i] = 1

	for i in range(200):
		tags[14000 + i] = 1

	tags = np.expand_dims(tags, axis=1)
	return np.concatenate((normal_data, tags), axis=1)


def create_sin_noise(normal_data):
	'''
	对正常的数据加入正弦噪声
	:param normal_data:
	:return:
	'''
	maxs = np.max(normal_data, axis=0)

	x = np.arange(-2 * np.pi, 2 * np.pi, 0.01)

	# 添加Class标签
	tags = np.zeros(normal_data.shape[0])

	# 增加的第二处异常 正弦型 9 10 11 12
	for j in range(9, 13):
	    max_j = maxs[j]
	    max_sin_val_j = max_j * np.sin(x)
	    for i in range(90):
	        normal_data[14000 + i, j] = max_sin_val_j[j]
			# tags[14000 + i] = 1

	for i in range(90):
		tags[14000 + i] = 2

	tags = np.expand_dims(tags, axis=1)
	return np.concatenate((normal_data, tags), axis=1)

def create_sin_mutiple_extension_noise(normal_data):
	'''
	正常数据加入扩展型噪声,最大值正弦值与原值相乘
	:param normal_data:
	:return:
	'''
	maxs = np.max(normal_data, axis=0)
	x_to_be_noised_indices = [0, 1, 2, 3, 9, 10, 11, 12]

	x = np.arange(-2 * np.pi, 2 * np.pi, 0.01)

	# 添加Class标签
	tags = np.zeros(normal_data.shape[0])

	for j in range(len(x_to_be_noised_indices)):
		y_max = maxs[x_to_be_noised_indices[j]]

		y = y_max * np.sin(x)
		for i in range(90):
			normal_data[17000 + i, x_to_be_noised_indices[j]] = y[i] * normal_data[17000 + i, x_to_be_noised_indices[j]]

	for i in range(90):
		tags[17000 + i] = 3

	tags = np.expand_dims(tags, axis=1)
	return np.concatenate((normal_data, tags), axis=1)


def create_sawtooth_noise(normal_data):

	maxs = np.max(normal_data, axis=0)
	mins = np.min(normal_data, axis=0)
	means = np.mean(normal_data, axis=0)

	x_to_be_noised_indices = [0, 1, 2, 3, 9, 10, 11, 12]
	# 添加Class标签
	tags = np.zeros(normal_data.shape[0])

	for j in range(len(x_to_be_noised_indices)):
		mean = means[x_to_be_noised_indices[j]]
		y_max = maxs[x_to_be_noised_indices[j]]
		y_min = mins[x_to_be_noised_indices[j]]

		for i in range(120):
			normal_data[19000 + i, x_to_be_noised_indices[j]] = normal_data[19000 + i, x_to_be_noised_indices[j]] + y_max

		for i in range(90):
			normal_data[10000 + i, x_to_be_noised_indices[j]] = normal_data[10000 + i, x_to_be_noised_indices[j]] - mean

		for i in range(90):
			normal_data[500 + i, x_to_be_noised_indices[j]] = normal_data[500 + i, x_to_be_noised_indices[j]] + y_min



	for i in range(120):
		tags[19000 + i] = 4

	for i in range(90):
		tags[10000 + i] = 4
		tags[500 + i] = 4

	tags = np.expand_dims(tags, axis=1)
	return np.concatenate((normal_data, tags), axis=1)

if __name__ == '__main__':
	rail_df = pd.read_csv('../dataset/origin-railway-data/20180608-848-0-GJHS-396-400.csv')

	# "id","KM","Meters","Flags","Event","Lprf(mm)","Rprf(mm)","Laln(mm)",\
	# "Raln(mm)","Rage(mm)","Cant(mm)","Xlvl(mm)","Wrap_1(mm)","Cvtr(radpkm)",
	# "Lacc_1(g)","Vacc(g)","LprfLW_3(mm)","RprfLW_3(mm)","LalnLW_3(mm)","RalnLW_3(mm)"
	# print(rail_df.head(5))

	rail_data = rail_df.values
	# print('rail_data shape: ', rail_data.shape)

	x_indices = [5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
	x_data = rail_data[:, x_indices]

	# print(x_data[0 : 4, :])
	# print('x_data shape: ', x_data.shape)

	# maxs = np.max(x_data, axis=0)
	# print('maxs.shape: ', maxs.shape)

	# tag = np.zeros(x_data.shape[0])
	# print(tag.shape)

	columns = 'Lprf(mm), Rprf(mm), Laln(mm), Raln(mm), Rage(mm), Cant(mm), Xlvl(mm), ' \
	          'Wrap_1(mm), Lacc_1(g), Vacc(g), LprfLW_3(mm), RprfLW_3(mm), LalnLW_3(mm), RalnLW_3(mm), Class';

	# # base_1  直线型异常
	# file_name_base1_1 = '../dataset/processed-railway-data/20180608-848-0-GJHS-396-400-processed-linear-anomaly1.csv'
	# x_y_data = create_linear_noise(x_data)
	# np.savetxt(file_name_base1_1, x_y_data, delimiter=',', header=columns)
	# print('OK!')

	# base_1  正弦异常
	# file_name_base1_2 = '../dataset/processed-railway-data/20180608-848-0-GJHS-396-400-processed-sin-anomaly2.csv'
	# x_y_data = create_sin_noise(normal_data=x_data)
	# print('x_y_data.shape', x_y_data.shape)
	# print(np.where(x_y_data[:, 14] == 2)[:])
	# np.savetxt(file_name_base1_2, x_y_data, delimiter=',', header=columns)
	# print('OK!')

	# # base_1  正弦乘性异常
	# file_name_base1_3 = '../dataset/processed-railway-data/20180608-848-0-GJHS-396-400-processed-sin-multiple_extension_anomaly3.csv'
	# x_y_data = create_sin_mutiple_extension_noise(normal_data= x_data)
	# # print('x_y_data.shape', x_y_data.shape)
	# # print(np.where(x_y_data[:, 14] == 3)[:])
	# np.savetxt(file_name_base1_3, x_y_data, delimiter=',', header=columns)
	# print('OK!')


	# # base_1  锯齿异常
	# file_name_base1_4 = '../dataset/processed-railway-data/20180608-848-0-GJHS-396-400-processed-sawtooth_anomaly4.csv'
	# x_y_data = create_sawtooth_noise(normal_data=x_data)
	# print('x_y_data.shape', x_y_data.shape)
	# print(np.where(x_y_data[:, 14] == 4))
	# np.savetxt(file_name_base1_4, x_y_data, delimiter=',', header=columns)
	# print('OK!')





